<!--
 * @Author: your name
 * @Date: 2022-04-09 14:33:18
 * @LastEditTime: 2022-04-09 14:38:38
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\如何截取字符串220409.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>substr</title>
</head>

<body>
  <script>
    
    //start 是一个字符的索引。首字符的索引为 0，最后一个字符的索引为 字符串的长度减去1
    var str = "abcdefghij";
    //substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符。
    console.log("(1,2): " + str.substr(1, 2)); // (1,2): bc
    //负数表示strLength + start 10
    console.log("(-3,2): " + str.substr(-3, 2)); // (-3,2): hi
    console.log("(-3): " + str.substr(-3)); // (-3): hij
    console.log("(1): " + str.substr(1)); // (1): bcdefghij
    console.log("(-20, 2): " + str.substr(-20, 2)); // (-20, 2): ab
    console.log("(20, 2): " + str.substr(20, 2)); // (20, 2):截取为空
  </script>
</body>

</html>